#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 21-3-17 下午2:35
# @Author  : zgd
# @Site    : 
# @File    : commons.py
# @Software: PyCharm

#自定义过滤器，实现热门新闻
from curses import wrapper

from flask import current_app
from flask import g
from flask import jsonify
from flask import session

from info.utils.response_code import RET


def hot_news_filter(index):
    if index  == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""

# 定义登陆装饰器，封装用户session信息

def user_login_data(view_func):
    from functools import wraps
    @wraps(view_func)
    def wrapper(*args,**kwargs):
        user_id = session.get("user_id")
        user = None
        if user_id:
            try:
                from info.models import User
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
                return jsonify(errno=RET.DBERR, errmsg="查询用户失败")
            #ｕｓｅｒ数据封装到g对象
        g.user = user
        return view_func(*args,**kwargs)
    return wrapper
















